import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  ParseIntPipe,
  Post,
  Req,
} from '@nestjs/common';
import { UserService } from './user.service';
import { User } from './user.entity';
import { DeleteResult } from 'typeorm';
import { wrapperResponse } from 'src/utils';

@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) {}

  /**
   * 通过 token 获取用户信息
   */
  @Get('getUserInfoByToken')
  getUserByToken(@Req() request): Promise<User> {
    return wrapperResponse(
      this.userService.findOneById(request.user.userid),
      '获取用户成功',
    );
  }
  /**
   * 获取全部 user
   */
  @Get('/:id')
  /**
   * 两种写法 @Param() params 和 @Param('id') id
   * @Param('id', ParseIntPipe) 将字符串的 id 转换为 number 类型
   */
  getUserById(@Param('id', ParseIntPipe) id: number): Promise<User> {
    return this.userService.findOneById(id);
  }
  /**
   * 获取全部 user
   */
  @Get()
  getAllUser(): Promise<User[]> {
    return this.userService.findAll();
  }

  /**
   * 创建 user
   */
  @Post()
  createUser(@Body() body): Promise<User> {
    return this.userService.create(body);
  }
  /**
   * 删除 user
   */
  @Delete(':id')
  deleteUser(@Param('id', ParseIntPipe) id: number): Promise<DeleteResult> {
    console.log('id', id);
    return this.userService.delete(id);
  }
}
